package cn.kgc.crowdfundingadmin2.service;

import cn.kgc.crowdfundingadmin2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin2.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin2.pojo.Admin;
import cn.kgc.crowdfundingadmin2.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Resource
    AdminMapper adminMapper;

    @Override
    public Admin login(String loginAcct, String userPswd) {
        Admin admin=null;
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);
        List<Admin> list=adminMapper.selectByExample(example);
        if (CrowdFundingUtils.collectionEffective(list)){
            admin=list.get(0);
            String md5=CrowdFundingUtils.md5(userPswd);
            if (md5.equals(admin.getUserpswd())){
                //登录成功
                return  admin;
            }else {
                //登录失败
                admin=null;
            }

        }
        return admin;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id desc");
        //keyword没有值查全部,优质根据账号或者名称或者邮箱地址模糊查询
        if (CrowdFundingUtils.stringEffective(keyword)) {
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria1=example.createCriteria();
            criteria1.andLoginacctLike("%"+keyword+"%");
            AdminExample.Criteria criteria2=example.createCriteria();
            criteria1.andUsernameLike("%"+keyword+"%");
            AdminExample.Criteria criteria3=example.createCriteria();
            criteria1.andEmailLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);

            List<Admin> list=adminMapper.selectByExample(example);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        } else {
            //查询全部
            List<Admin> list = adminMapper.selectByExample(null);
            PageInfo<Admin>pageInfo=new PageInfo<>(list);
            return pageInfo;
        }

    }

    @Override
    public void remove(List<Integer> admins) {
        //delete from 表 where id in (1,2,3)
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andIdIn(admins);
        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd=admin.getUserpswd();
        userPswd=CrowdFundingUtils.md5(userPswd);
        admin.setUserpswd(userPswd);
        //执行保存
        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String userPswd = admin.getUserpswd();
            userPswd = CrowdFundingUtils.md5(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}
